import pymysql


def get_db_con():
    con = pymysql.connect(host="localhost", port=3306,
                          user="root", passwd="123456",
                          db="flask")

    return con
#查询
def query_db(sql):
    con = get_db_con()
    cursor = con.cursor()
    cursor.execute(sql)
    data = cursor.fetchall()
    cursor.close()
    con.commit()
    con.close()
    return data

def change_db(sql):
    con = get_db_con()
    cursor = con.cursor()
    try:
        cursor.execute(sql)
        data = cursor.fetchall()
        con.commit()
    except Exception as e:
        con.rollback()
    return data

if __name__ == "__main__":
    # pass
    sql = 'SELECT*FROM flask.my_user WHERE username="mahaining"'
    c=change_db(sql)
    d=[]
    for i in c:
        name=i[1]
        pwd=i[2]
        c={name:pwd}
        d.append(c)
    print(d)